<template>
    <el-dialog
        :visible.sync="dialogVisible"
        title="修改积分"
        :close-on-press-escape="false"
        :close-on-click-modal="false"
        width="500px"
    >
        <el-form ref="formRef" :model="formData" :rules="rules" label-width="80px">
            <el-form-item label="积分数" prop="num">
                <el-input-number
                    v-model="formData.num"
                    controls-position="right"
                />
                <div class="form-tips">负数为减积分，正数为加积分</div>
            </el-form-item>

            <el-form-item label="备注" prop="remark">
                <el-input v-model="formData.remark"/>
            </el-form-item>

            <el-form-item>
                <el-popconfirm :title="'确定修改积分吗？'" @onConfirm="submitForm()">
                    <el-button slot="reference" type="primary">立即修改</el-button>
                </el-popconfirm>
            </el-form-item>
        </el-form>
    </el-dialog>
</template>

<script>
import {CreditApi} from '@/api/user/credit'

export default {
    name: 'UserCreditUpdate',
    components: {},
    data() {
        return {
            formData: {
                uid: 0,
                num: 0,
                remark: ''
            },
            rules: {
                num: [
                    {required: true, message: '请填写积分', trigger: 'blur'}
                ],
                remark: [
                    {required: true, message: '请填写备注', trigger: 'blur'}
                ]
            },
            dialogVisible: false
        }
    },
    methods: {
        // 显示组件
        showUpdate(data) {
            this.dialogVisible = true
            this.formData.uid = data.uid
            if (this.$refs['formRef'] !== undefined) {
                this.$refs['formRef'].resetFields()
            }
        },

        // 提交表单
        submitForm() {
            const that = this
            that.$refs['formRef'].validate((valid) => {
                if (valid) {
                    CreditApi.updateCredit(that.formData)
                        .then((response) => {
                            that.dialogVisible = false
                            that.$emit('refresh')
                        })
                }
            })
        }
    }
}
</script>
